package com.xms.schema.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xms.core.model.PagedList;
import com.xms.schema.entity.RelationShipView;
import com.xms.schema.service.relationship.IRelationShipViewFinderService;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author migo
 * @since 2021-08-27
 */
@RestController
@RequestMapping("/{org}/api/schema/relationship")
public class RelationShipFinderController extends ApiBaseController {
    private IRelationShipViewFinderService _relationShipViewFinderService;
    @Autowired
    public RelationShipFinderController(IRelationShipViewFinderService relationShipViewFinderService){
        _relationShipViewFinderService = relationShipViewFinderService;
    }

    @ApiOperation("查询实体关系")
    @GetMapping("list")
    public JsonResultObject get(@RequestParam(value="entityid", required = true) UUID entityId,
                                @RequestParam(value="type", required = false) Integer type,
                                @RequestParam(name="getall", required = false) Boolean getall,
                                @RequestParam(name="page", required = false) Integer page,
                                @RequestParam(name="pagesize", required = false) Integer pagesize)
    {
        if (UUIDUtil.isEmpty(entityId))
        {
            return JNotSpecifiedParams("entityid");
        }
        LambdaQueryWrapper<RelationShipView> lambdaQueryWrapper = getSortedQueryWrapper("name", "asc");

        if(type != null){
            if(type == 1){
                lambdaQueryWrapper.eq(RelationShipView::getReferencedentityid, entityId);
            }
            else{
                lambdaQueryWrapper.eq(RelationShipView::getReferencingentityid, entityId);
            }
        }
        if (getall != null && getall) {
            return JOk(_relationShipViewFinderService.list(lambdaQueryWrapper));
        }
        else if(page == null || pagesize == null){
            return JNotSpecifiedParams("page","pagesize");
        }
        page = (page <= 0 ? 1 : page);
        pagesize = (pagesize <= 0 ? 10 : pagesize);
        IPage<RelationShipView> datas = _relationShipViewFinderService.page(new Page<>(page, pagesize), lambdaQueryWrapper);
        PagedList<RelationShipView> result = new PagedList<>(datas.getRecords(), page, pagesize, datas.getTotal(), datas.getPages());

        return JOk(result);
    }

    @ApiOperation("查询关系元数据")
    @GetMapping("getbyid/{id}")
    public JsonResultObject getById(@PathVariable(value="id", required = true) UUID id)
    {
        if (UUIDUtil.isEmpty(id))
        {
            return JNotSpecifiedParams("id");
        }
        return JOk(_relationShipViewFinderService.getById(id));
    }

    @ApiOperation("查询关系元数据")
    @GetMapping("getbyname/{name}")
    public JsonResultObject getByName(@PathVariable(value="name", required = true) String name)
    {
        if (name.isEmpty())
        {
            return JNotSpecifiedParams("name");
        }
        return JOk(_relationShipViewFinderService.getByName(name));
    }

    @ApiOperation("实体引用关系列表")
    @GetMapping("getreferencing/{referencingentityid}")
    public JsonResultObject getReferencing(@PathVariable(value="referencingentityid", required = true) UUID referencingEntityId)//, @RequestParam(value = "referencedEntityId", required = false) UUID referencedEntityId)
    {
        List<RelationShipView> result = _relationShipViewFinderService.getByReferencingEntityId(referencingEntityId);//, referencedEntityId

        return JOk(result);
    }

    @ApiOperation("实体被引用关系列表")
    @GetMapping("getreferenced/{referencedentityid}")
    public JsonResultObject getReferenced(@PathVariable(value="referencedentityid", required = true) UUID referencedEntityId) //, UUID referencingEntityId
    {
        List<RelationShipView> result = _relationShipViewFinderService.getByReferencedEntityId(referencedEntityId);

        return JOk(result);
    }
}
